//
//  CZQHaveTimeView.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/26.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQHaveTimeView.h"
@implementation CZQTimeView : UIView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        /**
         星期几
         */
        
        UILabel *dayLable = [UILabel new];
        dayLable.text = @"星期一";
        dayLable.textColor = BlackColor;
        dayLable.font = SmallFont;
        self.dayLable = dayLable;
        [self addSubview:dayLable];
        
        /**
         放时间的View
         */
        UIView *bgView = [UIView new];
        [self addSubview:bgView];
        bgView.hidden = YES;
        self.bgView = bgView;
        
        [dayLable mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.mas_equalTo(self);
            make.width.mas_equalTo(40);
        }];
        
        UIView *reView = nil;
        
        for (int i = 0; i < 32; i ++) {
            UIView *view = [UIView new];
            view.backgroundColor = ButtonBackGroundColor;
            [self addSubview:view];
            [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                if (reView == nil) {
                    make.centerY.mas_equalTo(self.dayLable);
                    make.left.mas_equalTo(self.dayLable.mas_right).offset(10);
                    make.height.mas_equalTo(10);
                }else
                {
                    make.centerY.mas_equalTo(self.dayLable);
                    make.left.mas_equalTo(self.dayLable.mas_right).offset(10);
                    make.width.height.mas_equalTo(reView);
                    if (i == 31) {
                        make.right.mas_equalTo(self).offset(-10);
                    }
                    
                }
                
                
                
            }];
            
            reView = view;
        }
        
        //时间
        [bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.dayLable.mas_bottom);
            make.left.mas_equalTo(self.dayLable.mas_right).offset(10);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(10);
            make.bottom.mas_equalTo(self).offset(-5);
            
        }];
        UILabel *lab1 = nil;
        for (int i = 0; i < 9; i ++) {
            UILabel *lab = [UILabel new];
            lab.text = [NSString stringWithFormat:@"%ld",i *2 + 7];
            lab.font = SmallFont;
            [self.bgView addSubview:lab];
            [lab mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                if (lab1 == nil) {
                    make.top.left.bottom.mas_equalTo(self.bgView);
                    
                }else
                {
                    make.top.mas_equalTo(self.bgView);
                    make.left.mas_equalTo(lab1.mas_right);
                    make.height.width.mas_equalTo(lab1);
                    if (i == 7) {
                        make.right.mas_equalTo(self.bgView).offset(-10);
                        
                    }
                    
                }
                
            }];
            
            lab1 = lab;
        }
        
    }
    return self;
}

@end
@implementation CZQHaveTimeView
-(id)initWithFrame:(CGRect)frame{
    
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
//        self.backgroundColor = CSWhiteColor;
        
        UIView *alphaView = [[UIView alloc] init];
        alphaView.backgroundColor = [CSBlackColor colorWithAlphaComponent:0.4];
        [self addSubview:alphaView];
        self.alphaView = alphaView;
        
        UIView *whiteView = [[UIView alloc] init];
        whiteView.backgroundColor = CSWhiteColor;
        [self addSubview:whiteView];
        self.whiteView = whiteView;
        
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"近七天可约时间";
        titleLabel.font = BoldBigFont;
        [whiteView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UIButton *closeBtn = [[UIButton alloc] init];
        [closeBtn setImage:@"pop_icon_close"];
        [closeBtn addTarget:self action:@selector(hiddenWithAnimation)];
        [whiteView addSubview:closeBtn];
        self.closeBtn = closeBtn;
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = LineBackGroundColor;
        [whiteView addSubview:lineView];
        self.lineView = lineView;
        
        
        /**
         有时间
         */
        UIButton *haveTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [haveTimeBtn setImage:@""];
        [haveTimeBtn setTitle:@"可约时间"];
        [haveTimeBtn setTitleColor:ButtonBackGroundColor];
        haveTimeBtn.titleLabel.font = SmallFont;
        
        [whiteView addSubview:haveTimeBtn];
        self.haveTimeBtn = haveTimeBtn;
        
        /**
         无时间
         */
        UIButton *noTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [noTimeBtn setImage:@""];
        [noTimeBtn setTitle:@"不可约时间"];
        [noTimeBtn setTitleColor:GrakColor];
        noTimeBtn.titleLabel.font = SmallFont;
        
        [whiteView addSubview:noTimeBtn];
        self.noTimeBtn = noTimeBtn;
        
        
        MOFSBottomView *bottomView = [[MOFSBottomView alloc] init];
        [bottomView.cancleBtn setTitle:@"需要帮助"];
        [bottomView.sureBtn setTitle:@"立刻购买"];
        [whiteView addSubview:bottomView];
        self.bottomView = bottomView;
        
        __weak __typeof(self) weakSelf = self;
        
        self.bottomView.cancelBlock = ^ {
            
            if (weakSelf.cancelBlock) {
                
                
                weakSelf.cancelBlock();
            }
        };
        
        self.bottomView.commitBlock = ^ {
            
            if (weakSelf.commitBlock) {
                
                
                
            }
        };
        
        
        
        [self setConstraints];
    }
    
    return self;
}
-(void)setConstraints{
    
    [self.alphaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    [self.whiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(0);
        make.right.mas_equalTo(self).offset(0);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.whiteView).offset(20);
        make.centerX.mas_equalTo(self.whiteView);
    }];
    
    [self.closeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.whiteView).offset(-10);
        make.top.mas_equalTo(self.whiteView).offset(10);
    }];
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10);
        make.left.width.mas_equalTo(self.whiteView);
        make.height.mas_equalTo(1);
    }];
    
    CZQTimeView *timeView1;
    
    for (int i = 0; i < 7; i ++) {
        CZQTimeView *timeView = [CZQTimeView new];
        timeView.dayLable.text = [NSString stringWithFormat:@"星期%ld",i];
        [self addSubview:timeView];
        
        [timeView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            if (timeView1 == nil) {
                make.top.mas_equalTo(self.lineView.mas_bottom).offset(10);
                make.left.mas_equalTo(self.whiteView).offset(10);
                make.right.mas_equalTo(self.whiteView).offset(-10);
            }else
            {
                make.top.mas_equalTo(timeView1.mas_bottom).offset(10);
                make.left.mas_equalTo(self.whiteView).offset(10);
                make.right.mas_equalTo(self.whiteView).offset(-10);
                if (i == 6) {
                    make.bottom.mas_equalTo(self.haveTimeBtn.mas_top).offset(-10);
                    timeView.bgView.hidden = NO;
                }
                
            }
            
            
        }];
        
        timeView1 = timeView;
        
        
        
    }
    
    /**
     有时间
     */
    
    [self.haveTimeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        //        make.top.mas_equalTo(self.timeView.mas_bottom).offset(30);
        
        make.right.mas_equalTo(self.noTimeBtn.mas_left).offset(-15);
        
    }];

    
    /**
     无时间
     */
    [self.noTimeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.haveTimeBtn);
        
        make.right.mas_equalTo(self.whiteView).offset(-10);
        
    }];

    
    [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.haveTimeBtn.mas_bottom).offset(20);
        
        make.left.width.bottom.mas_equalTo(self.whiteView);
        
        make.bottom.mas_equalTo(self.whiteView);
    }];
    
}
- (void)showWithAnimation {
    
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    //控制整个功能是否启用。
    manager.enable = NO;
    
    [self addViews];
    self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    CGFloat height = self.whiteView.frame.size.height;
    self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT - height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }];
    
}

- (void)hiddenWithAnimation {
    CGFloat height = self.whiteView.frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self hiddenViews];
    }];
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    //控制整个功能是否启用。
    manager.enable = YES;
    
    [self removeFromSuperview];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
